package basic.study.leetcode;

/**
 * @ClassName Mid2
 * @Description
 * @Company inspur
 * @Author Kevin
 * @Date 2020/6/16 11:14
 * @Version 1.0
 */
public class Mid2 {
    class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            if(l1 == null || l2 == null) {
                return null;
            }
            ListNode root = new ListNode(-1);
            ListNode node = root;
            int carry = 0;
            while (l1 != null || l2 != null || carry != 0) {
                int val1 = l1 != null?l1.val:0;
                int val2 = l2 != null?l2.val:0;
                int sumVal = val1 + val2 +carry;
                carry = sumVal / 10;

                ListNode temp = new ListNode(sumVal % 10);
                node.next = temp;
                node = temp;

                if (l1 != null) l1 = l1.next;
                if (l2 != null) l2 = l2.next;
            }
            return root.next;
        }
    }

    /**
     * Definition for singly-linked list.
     */
    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }}
}
